<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="项目id" prop="projectId"
                :rules="[
                 ]">
              <el-input v-model="inputForm.projectId" placeholder="请填写项目id"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="采购方式" prop="procurementMethod"
                :rules="[
                 ]">
                <el-select v-model="inputForm.procurementMethod" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('procurement_method')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="预估合同额" prop="estimatedContractAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.estimatedContractAmount" placeholder="请填写预估合同额"     ></el-input>
           </el-form-item>
        </el-col>
  
        <el-col :span="12">
            <el-form-item label="评标方法" prop="evaluationMethod"
                :rules="[
                 ]">
                <el-select v-model="inputForm.evaluationMethod" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('evaluation_method')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="附件" prop="attachment"
                :rules="[
                 ]">
                <file-upload v-model="inputForm.attachment" tip="单个文件不要超过200M"></file-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="项目概况" prop="projectOverview"
                :rules="[
                 ]">
              <el-input v-model="inputForm.projectOverview" placeholder="请填写项目概况"  type='textarea'  :rows='6'  ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import projectBidService from '@/api/bid/projectBidService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          projectId: '',
          procurementMethod: '',
          estimatedContractAmount: '',
          projectOverview: '',
          evaluationMethod: '',
          attachment: '',
          fileName:'',
        }
      }
    },
    components: {
    },
    watch: {
    'inputForm.attachment': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm.fileName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    }
  },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建项目投标`
        } else if (method === 'edit') {
          this.title = '修改项目投标'
        } else if (method === 'view') {
          this.title = '查看项目投标'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            projectBidService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            projectBidService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
